package com.yonyou.common.service.messagecenter.rabbitmq.service;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yonyou.common.service.messagecenter.dao.TtLostMessageMapper;
import com.yonyou.common.service.messagecenter.dao.TtMessageHistoryMapper;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.MessageProtocol;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.MessageProtocolDto;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtLostMessageHistory;
import com.yonyou.common.service.messagecenter.rabbitmq.entity.TtMessageHistory;
import com.yonyou.common.service.messagecenter.util.MqConsts;

import tk.mybatis.mapper.entity.Example;

@Service
public class MessageHistoryService {

	@Autowired
	private TtMessageHistoryMapper historyMapper;
	@Autowired
	private TtLostMessageMapper lostHistoryMapper;
	
    public TtMessageHistory getMessageHistory(MessageProtocol message,boolean reSend,String exchange){
    	TtMessageHistory record =new TtMessageHistory();
		record.setBody(message.getMessagecontent());
    	record.setCreateDate(new Date(System.currentTimeMillis()));
    	record.setStatus(MqConsts.PRODUCE_STATUS_FINISH);//PRODUCE_STATUS_START
    	record.setSign(message.getSign());
    	record.setAppid(message.getAppid());
    	record.setExchange(exchange);
    	String[] datas=message.getMessagecontent().split(MqConsts.MES_PLACE_HOLDER);
    	String data=datas[0];
    	record.setMessageid(message.parseMessageId(data));
    	record.setReSend(Boolean.valueOf(reSend).toString());
    	return record;
    }
    public TtMessageHistory getMessageHistory(MessageProtocolDto message){
    	TtMessageHistory record =new TtMessageHistory();
		record.setBody(message.getData());
    	record.setCreateDate(new Date(System.currentTimeMillis()));
    	record.setStatus(MqConsts.PRODUCE_STATUS_FINISH);//PRODUCE_STATUS_START
    	record.setSign(message.getSign());
    	record.setAppid(message.getAppid());
    	record.setExchange(message.getEndPointName());
    	String[] datas=message.getData().split(MqConsts.MES_PLACE_HOLDER);
    	String data=datas[0];
    	record.setMessageid(MessageProtocol.parseMessageId(data));
    	record.setReSend("false");
    	return record;
    }
    @Transactional(rollbackFor=Exception.class)//annotationDrivenTransactionManager
    public void insertHistory(TtMessageHistory record){
    	historyMapper.insert(record);
    }

    @Transactional(rollbackFor=Exception.class)
    public void insertBatch(List<TtMessageHistory> list){
    	historyMapper.insertBatch(list);
//		for(TtMessageHistory record:list){
//	    	historyMapper.insert(record);
//		}
	}
    @Transactional(rollbackFor=Exception.class)
    public void updateHistorySuccess(TtMessageHistory record){
    	record.setStatus(MqConsts.PRODUCE_STATUS_FINISH);
    	record.setUpdateDate(new Date(System.currentTimeMillis()));
    	historyMapper.updateByPrimaryKeySelective(record);
    }
    @Transactional(rollbackFor=Exception.class)
    public void updateHistoryFail(TtMessageHistory record){
    	record.setStatus(MqConsts.PRODUCE_STATUS_FAIL);
    	record.setUpdateDate(new Date(System.currentTimeMillis()));
    	historyMapper.updateByPrimaryKey(record);
    }
    
    /**
     * 查询历史消息
     * 
     * @param page
     * @param pageSize
     * @return
     */
    public PageInfo<TtMessageHistory> getAllMessageByPage(int page,int pageSize,String messageContent,String sign){
    	Example example = new Example(TtMessageHistory.class);
    	example.createCriteria()
    	        .andCondition(StringUtils.isEmpty(messageContent)?"1=1":"body like '%"+messageContent+"%'")
    			.andCondition(StringUtils.isEmpty(sign)?"1=1":"sign='"+sign+"'");
    	example.orderBy("id").desc(); 
    	PageInfo<TtMessageHistory> pages= PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> historyMapper.selectByExample(example));
    	
    	return pages;
    }
    public PageInfo<TtMessageHistory> getAllLostMessageByPage(int page,int pageSize,String messageContent,String sign){
    	Example example = new Example(TtLostMessageHistory.class);
    	example.createCriteria()
    	        .andCondition(StringUtils.isEmpty(messageContent)?"1=1":"body like '%"+messageContent+"%'")
    			.andCondition(StringUtils.isEmpty(sign)?"1=1":"sign='"+sign+"'");
    	example.orderBy("id").desc(); 
    	PageInfo<TtMessageHistory> pages= PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> lostHistoryMapper.selectByExample(example));
    	
    	return pages;
    }
    
    
}
